#include <QCamera>
#include <QCameraInfo>
#include <QGraphicsVideoItem>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMessageBox>
#include <QInputDialog>
#include <QLineEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QTabWidget>
#include <QResizeEvent>
#include <QGraphicsSimpleTextItem>
#include <QTranslator>
#include <QFileDialog>

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowState(Qt::WindowMaximized);
    font = new QFont("SimSun", 20, QFont::Bold);

    translator = new QTranslator;
    qApp->installTranslator(translator);

    mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);

    mainLayout = new QGridLayout(mainWidget);
    mainWidget->setLayout(mainLayout);

    scene = new QGraphicsScene(this);
    view = new QGraphicsView(scene, this);
    mainLayout->addWidget(view, 0, 0, 2, 1);

    captureButton = new QPushButton();
    captureButton->setFixedWidth(200);
    captureWidget = new QTabWidget;
    captureWidget->setFixedWidth(200);
    iconWidget = new QWidget;
    langWidget = new QWidget;
    captureWidget->addTab(iconWidget, "");
    captureWidget->addTab(langWidget, "");
    mainLayout->addWidget(captureButton, 0, 1);
    mainLayout->addWidget(captureWidget, 1, 1);

    iconLayout = new QVBoxLayout(iconWidget);
    iconLayout->setContentsMargins(20, 20, 20, 20);
    iconWidget->setLayout(iconLayout);

    hatButton = new QPushButton();
    hatButton->setFixedSize(120, 120);
    hatButton->setIcon(QIcon(":/images/hat.png"));
    hatButton->setIconSize(QSize(100, 100));
    iconLayout->addWidget(hatButton, 0, Qt::AlignHCenter);
    mustacheButton = new QPushButton();
    mustacheButton->setFixedSize(120, 120);
    mustacheButton->setIcon(QIcon(":/images/mustache.png"));
    mustacheButton->setIconSize(QSize(100, 100));
    iconLayout->addWidget(mustacheButton, 0, Qt::AlignHCenter);
    textButton = new QPushButton();
    textButton->setFixedSize(120, 120);
    textButton->setIcon(QIcon(":/images/text.png"));
    textButton->setIconSize(QSize(100, 100));
    iconLayout->addWidget(textButton, 0, Qt::AlignHCenter);
    delButton = new QPushButton();
    delButton->setFixedSize(120, 20);
    iconLayout->addWidget(delButton, 0, Qt::AlignHCenter);
    iconLayout->addStretch();

    langLayout = new QVBoxLayout(langWidget);
    langLayout->setContentsMargins(20, 20, 20, 20);
    langWidget->setLayout(langLayout);

    enButton = new QRadioButton("English");
    enButton->setChecked(true);
    langLayout->addWidget(enButton, 0, Qt::AlignHCenter);
    chsButton = new QRadioButton("中   文");
    langLayout->addWidget(chsButton, 0, Qt::AlignHCenter);
    langLayout->addStretch();

    connect(hatButton, SIGNAL(clicked(bool)), this, SLOT(addHat()));
    connect(mustacheButton, SIGNAL(clicked(bool)), this, SLOT(addMustache()));
    connect(textButton, SIGNAL(clicked(bool)), this, SLOT(addText()));
    connect(delButton, SIGNAL(clicked(bool)), this, SLOT(delItem()));
    connect(enButton, SIGNAL(toggled(bool)), this, SLOT(selectEnglish()));
    connect(chsButton, SIGNAL(toggled(bool)), this, SLOT(selectChinese()));
    connect(captureButton, SIGNAL(clicked(bool)), this, SLOT(capture()));

    hasCamera = checkCamera();

    warningItem = new QGraphicsSimpleTextItem;
    warningItem->setFont(*font);
    warningItem->setText(tr("Camera required"));
    if (hasCamera) {
        videoItem = new QGraphicsVideoItem;
        videoItem->setAspectRatioMode(Qt::IgnoreAspectRatio);
        videoItem->setPos(0, 0);
        videoItem->setSize(view->size());
        videoItem->setZValue(0);
        scene->addItem(videoItem);
        camera = new QCamera(QCameraInfo::defaultCamera(), this);
        camera->setViewfinder(videoItem);
        QTimer::singleShot(200, this, SLOT(startCamera()));         // http://blog.csdn.net/imred/article/details/54799266
    } else {
        QMessageBox::warning(this, tr("Warning"), tr("Camera required"));
        scene->addItem(warningItem);
    }
    retranslateUi();
}

MainWindow::~MainWindow()
{

}

void MainWindow::capture()
{
    QPixmap pixmap = QWidget::grab(view->geometry());
    QString filename = QFileDialog::getSaveFileName(this, tr("Save picture"), "", tr("Images (*.png)")); //https://bugreports.qt.io/browse/QTBUG-56599
    if (!filename.isEmpty()) {
        pixmap.save(filename, "png");
    }
}

void MainWindow::retranslateUi()
{
    captureButton->setText(tr("Capture"));
    captureWidget->setTabText(captureWidget->indexOf(iconWidget), tr("Icon && Text"));
    captureWidget->setTabText(captureWidget->indexOf(langWidget), tr("Language"));
    delButton->setText(tr("Delete"));
    warningItem->setText(tr("Camera required"));
}

void MainWindow::selectChinese()
{
    translator->load(":/translations/qtdemo_chs.qm");
    retranslateUi();
}

void MainWindow::selectEnglish()
{
    translator->load(":/translations/qtdemo_en.qm");
    retranslateUi();
}

void MainWindow::startCamera()
{
    camera->start();
}

void MainWindow::addHat()
{
    QGraphicsItem *item = scene->addPixmap(QPixmap(":/images/hat.png"));
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    item->setZValue(1);
}

void MainWindow::addMustache()
{
    QGraphicsItem *item = scene->addPixmap(QPixmap(":/images/mustache.png"));
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    item->setZValue(1);
}

void MainWindow::addText()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("Input Text"),
                                        tr("Text"), QLineEdit::Normal,
                                        "", &ok);
    if (ok && !text.isEmpty()) {
        QGraphicsItem *item = scene->addText(text, *font);
        item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
        item->setZValue(2);
    }
}

void MainWindow::delItem()
{
    QList<QGraphicsItem *> items = scene->selectedItems();
    if (items.count() == 1) {
        delete items.first();
    }
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    scene->setSceneRect(0, 0, view->width() - 2, view->height() - 2);
    if (hasCamera) {
        videoItem->setSize(view->size());
        videoItem->setPos(0, 0);
    }
}

bool MainWindow::checkCamera() const
{
    if (QCameraInfo::availableCameras().count() > 0) {
        return true;
    } else {
        return false;
    }
}
